package com.abel.quiz.controller;

import com.abel.quiz.common.vo.AjaxResp;
import com.abel.quiz.po.UserInfoPoCriteria;
import com.abel.quiz.services.UserInfoService;
import com.abel.quiz.vo.UserInfoDomain;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * Created by Administrator on 2017/8/13.
 */
@RequestMapping("admin/user")
@Controller
public class UserController extends BaseController{
    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping("list")
    public ModelAndView list(HttpServletRequest request, HttpServletResponse response){
        ModelAndView view = new ModelAndView("admin/user/list");

        return view;
    }

    @ResponseBody
    @RequestMapping("ajaxDataGrid")
    public String ajaxDataGrid(HttpServletRequest request, HttpServletResponse response,
                               @ModelAttribute UserInfoDomain searchArgs,
                               @RequestParam(value = "pageNO", defaultValue = "1")Integer pageNO,
                               @RequestParam(value = "pageSize", defaultValue = "10")Integer pageSiz){
        AjaxResp ajax = new AjaxResp(AjaxResp.CodeArgs.OK);
        UserInfoPoCriteria query = new UserInfoPoCriteria();
        UserInfoPoCriteria.Criteria criteria = query.createCriteria();
        if(StringUtils.isNotBlank(searchArgs.getLoginName())){
            criteria.andLoginNameLike(searchArgs.getLoginName());
        }
        if(searchArgs.getEnabled() != null){
            criteria.andEnabledEqualTo(searchArgs.getEnabled());
        }
        query.setOrderByClause("id");
        PageInfo pageInfo = userInfoService.selectPageByExample(query,pageNO, pageSiz);
        ajax.setData(pageInfo.getList());
        ajax.setTotal(pageInfo.getTotal());
        return ajax.toJson();
    }

    @RequestMapping("add")
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response){
        ModelAndView view = new ModelAndView("admin/user/add");

        return view;
    }

    @ResponseBody
    @RequestMapping("doSave")
    public String doSave(HttpServletRequest request, HttpServletResponse response,
                         @ModelAttribute UserInfoDomain domain){
        AjaxResp ajax = new AjaxResp(AjaxResp.CodeArgs.OK);

        domain.setCreateUser(getCurrentUserName());
        domain.setCreateTime(new Date());
        domain.setUpdateUser(domain.getCreateUser());
        domain.setUpdateTime(domain.getCreateTime());
        if(domain.getEnabled() == null){
            domain.setEnabled(0);
        }
        int result = userInfoService.insertDomain(domain);
        if(result == 0){
            ajax.setCode(AjaxResp.CodeArgs.NO);
        }else if(result < 0){
            ajax.setCode(result+"");
        }
        return ajax.toJson();
    }

    @RequestMapping("edit/{id}")
    public ModelAndView edit(HttpServletRequest request, HttpServletResponse response,
                             @PathVariable("id")String id){
        ModelAndView view = new ModelAndView("admin/user/edit");
        UserInfoDomain domain = userInfoService.selectDomainById(id);

        view.addObject("vo",domain);
        return view;
    }

    @ResponseBody
    @RequestMapping("doUpdate")
    public String doUpdate(HttpServletRequest request, HttpServletResponse response,
                           @ModelAttribute UserInfoDomain domain){
        AjaxResp ajax = new AjaxResp(AjaxResp.CodeArgs.OK);

        domain.setUpdateUser(getCurrentUserName());
        domain.setUpdateTime(new Date());
        if(domain.getEnabled() == null){
            domain.setEnabled(0);
        }
        int result = userInfoService.updateDomainByPrimaryKey(domain);
        if(result == 0){
            ajax.setCode(AjaxResp.CodeArgs.NO);
        }
        return ajax.toJson();
    }

    @ResponseBody
    @RequestMapping("doDel")
    public String doDel(HttpServletRequest request, HttpServletResponse response,
                        @RequestParam(value = "id")String id){
        AjaxResp ajax = new AjaxResp(AjaxResp.CodeArgs.OK);

        int result = userInfoService.deleteByPrimaryKey(id);
        if(result == 0){
            ajax.setCode(AjaxResp.CodeArgs.NO);
        }
        return ajax.toJson();
    }
}
